package com.jrx.anytxn.reporting.job.xib.config;

import com.jrx.anytxn.common.uitl.DateUtils;
import com.jrx.anytxn.param.entity.PrOrganizationTable;
import com.jrx.anytxn.param.service.system.IOrganizationTableService;
import com.jrx.anytxn.reporting.config.AnyTxnBatchProperties;
import com.jrx.anytxn.reporting.constant.ReportingConstant;
import com.jrx.anytxn.reporting.entity.MEastVStzfxxb;
import com.jrx.anytxn.reporting.job.xib.step.EntrustmentReportingListener;
import com.jrx.anytxn.reporting.job.xib.step.EntrustmentReportingProcessor;
import com.jrx.anytxn.reporting.job.xib.step.EntrustmentReportingWriter;
import com.jrx.anytxn.transaction.entity.AmLoanMainInfo;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.batch.MyBatisCursorItemReader;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.item.support.SynchronizedItemStreamReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SimpleAsyncTaskExecutor;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @comment: 受托支付信息任务定义
 * @author: liuzb
 * @date: 2022/8/23
 */
@Configuration
@EnableConfigurationProperties(AnyTxnBatchProperties.class)
@EnableBatchProcessing
public class EntrustmentReportingConfiguration {

    @Resource(name = "bizSqlSessionFactory")
    SqlSessionFactory sqlSessionFactory;
    @Autowired
    public JobBuilderFactory jobBuilderFactory;
    @Autowired
    public StepBuilderFactory stepBuilderFactory;
    @Autowired
    AnyTxnBatchProperties anyTxnBatchProperties;
    @Autowired
    EntrustmentReportingListener entrustmentReportingListener;
    @Autowired
    EntrustmentReportingProcessor entrustmentReportingProcessor;
    @Autowired
    EntrustmentReportingWriter entrustmentReportingWriter;
    @Autowired
    private IOrganizationTableService organizationTableService;

    @Bean
    public Job xibEntrustmentReportingJob() {
        return jobBuilderFactory.get("xibEntrustmentReportingJob")
                .incrementer(new RunIdIncrementer())
                .listener(entrustmentReportingListener)
                .start(xibEntrustmentReportingStep())
                .build();
    }

    @Bean
    public Step xibEntrustmentReportingStep() {
        return stepBuilderFactory.get("xibEntrustmentReportingStep")
                .<AmLoanMainInfo, MEastVStzfxxb>chunk(anyTxnBatchProperties.getReportingCustomerConfig().getChunkLimit())
                .reader(entrustmentReportingReader())
                .processor(entrustmentReportingProcessor)
                .writer(entrustmentReportingWriter)
                .taskExecutor(new SimpleAsyncTaskExecutor())
                .throttleLimit(anyTxnBatchProperties.getReportingCustomerConfig().getThrottleLimit())
                .build();
    }

    @Bean
    @StepScope
    public SynchronizedItemStreamReader<AmLoanMainInfo> entrustmentReportingReader() {
        PrOrganizationTable org = organizationTableService.findByOrgIdForBatch(ReportingConstant.DEFAULT_ORG_NUMBER);
        MyBatisCursorItemReader<AmLoanMainInfo> cursorItemReader = new MyBatisCursorItemReader<>();
        cursorItemReader.setSaveState(false);
        cursorItemReader.setSqlSessionFactory(sqlSessionFactory);
        cursorItemReader.setQueryId("com.jrx.anytxn.transaction.mapper.ext.ExtAmLoanMainInfoMapper.selectLoanInfoByPostingDate");
        Map<String, Object> map = new HashMap<>();
        map.put("postingDate", DateUtils.format(org.getToday()));
        map.put("tenantId", org.getTenantId());
        cursorItemReader.setParameterValues(map);
        SynchronizedItemStreamReader<AmLoanMainInfo> synchronizedItemStreamReader = new SynchronizedItemStreamReader<>();
        synchronizedItemStreamReader.setDelegate(cursorItemReader);
        return synchronizedItemStreamReader;
    }

}
